﻿namespace Brjnk.GpxExplorer.Workspace
{
    using System;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using Brjnk.GpxExplorer.Data.ImportExport;

    public class CustomExternalApplication : ExternalApplication
    {
        private ImageSource icon;

        private void RefreshIcon()
        {
            if (System.IO.File.Exists(path))
                icon = Brjnk.Common.IconsReader.GetIcon(path);
            else
                icon = null;
            base.OnPropertyChanged("Icon");
        }

        private string name;

        public string NameSerializable
        {
            get { return name; }
            set { name = value; base.OnPropertyChanged("NameSerializable"); base.OnPropertyChanged("Name"); }
        }

        private string path;

        public string PathSerializable
        {
            get { return path; }
            set { path = value; OnPropertyChanged("PathSerializable"); base.OnPropertyChanged("Path"); base.OnPropertyChanged("Warning"); RefreshIcon(); }
        }

        private ExternalApplicationType type;

        public ExternalApplicationType Type
        {
            get { return type; }
            set
            {
                type = value;
                switch (type)
                {
                    case ExternalApplicationType.Unknown:
                        writer = null;
                        break;

                    case ExternalApplicationType.DefaultGpx:
                        writer = new GpxWriter(false);
                        break;

                    case ExternalApplicationType.GcGpx:
                        writer = new GpxWriter(true);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("Neznámý ExternalApplicationType.");
                }
                OnPropertyChanged("Type");
                OnPropertyChanged("Writer");
                OnPropertyChanged("UseGcGpx");
            }
        }

        //Následující vlastnost slouží pouze pro bindování do Checkboxu, proto není serializována a její hodnota je určena z valstnosti Type
        [XmlIgnore]
        public bool? UseGcGpx
        {
            get
            {
                switch (type)
                {
                    case ExternalApplicationType.Unknown:
                        return null;
                    case ExternalApplicationType.DefaultGpx:
                        return false;
                    case ExternalApplicationType.GcGpx:
                        return true;
                    default:
                        throw new ArgumentOutOfRangeException("Neznámý ExternalApplicationType.");
                }
            }
            set
            {
                if (!value.HasValue) Type = ExternalApplicationType.Unknown;
                else if (value.Value) Type = ExternalApplicationType.GcGpx;
                else Type = ExternalApplicationType.DefaultGpx;
                OnPropertyChanged("UseGcGpx");
            }
        }

        public override bool IsAvailable
        {
            get { return true; }
        }

        public override bool Warning
        {
            get { return !System.IO.File.Exists(path); }
        }

        public override string Name
        {
            get { return name; }
        }

        public override System.Windows.Media.ImageSource Icon
        {
            get { return icon; }
        }

        public override string Path
        {
            get { return path; }
        }

        private IDataWriter writer;

        public override Data.ImportExport.IDataWriter Writer
        {
            get
            {
                return writer;
            }
        }
    }

    public enum ExternalApplicationType
    {
        Unknown, DefaultGpx, GcGpx
    }
}